package zzps.exeed.kernel.tool;

import java.io.Serial;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

public class FastList <T> implements List<T>, RandomAccess, Serializable {
    @Serial
    private static final long serialVersionUID = 8420861999177542078L;
    private final Class<?> clazz;
    private T[] elementData;
    private int size;


    @SuppressWarnings("unchecked")
    public FastList(Class<?> clazz, int capacity)
    {
        this.elementData = (T[]) Array.newInstance(clazz, capacity);
        this.clazz = clazz;
    }
    @Override
    public boolean add(T element)
    {
        if (size < elementData.length) {
            elementData[size++] = element;
        }
        else {
            // overflow-conscious code
            final var oldCapacity = elementData.length;
            final var newCapacity = oldCapacity << 1;
            @SuppressWarnings("unchecked")
            final var newElementData = (T[]) Array.newInstance(clazz, newCapacity);
            System.arraycopy(elementData, 0, newElementData, 0, oldCapacity);
            newElementData[size++] = element;
            elementData = newElementData;
        }

        return true;
    }

    @Override
    public T get(int index)
    {
        return elementData[index];
    }
    public T removeLast()
    {
        T element = elementData[--size];
        elementData[size] = null;
        return element;
    }
    @Override
    public boolean remove(Object element)
    {
        for (var index = size - 1; index >= 0; index--) {
            if (element == elementData[index]) {
                final var numMoved = size - index - 1;
                if (numMoved > 0) {
                    System.arraycopy(elementData, index + 1, elementData, index, numMoved);
                }
                elementData[--size] = null;
                return true;
            }
        }

        return false;
    }

    @Override
    public void clear()
    {
        for (var i = 0; i < size; i++) {
            elementData[i] = null;
        }

        size = 0;
    }
    @Override
    public int size()
    {
        return size;
    }

    @Override
    public boolean isEmpty()
    {
        return size == 0;
    }

    @Override
    public T set(int index, T element)
    {
        T old = elementData[index];
        elementData[index] = element;
        return old;
    }

    @Override
    public T remove(int index)
    {
        if (size == 0) {
            return null;
        }

        final T old = elementData[index];

        final var numMoved = size - index - 1;
        if (numMoved > 0) {
            System.arraycopy(elementData, index + 1, elementData, index, numMoved);
        }

        elementData[--size] = null;

        return old;
    }

    @Override
    public boolean contains(Object o)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public Iterator<T> iterator()
    {
        return new Iterator<>() {
            private int index;

            @Override
            public boolean hasNext()
            {
                return index < size;
            }

            @Override
            public T next()
            {
                if (index < size) {
                    return elementData[index++];
                }

                throw new NoSuchElementException("No more elements in FastList");
            }
        };
    }

    @Override
    public Object[] toArray()
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public <E> E[] toArray(E[] a)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean containsAll(Collection<?> c)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean addAll(Collection<? extends T> c)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean addAll(int index, Collection<? extends T> c)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean removeAll(Collection<?> c)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean retainAll(Collection<?> c)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public void add(int index, T element)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public int indexOf(Object o)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public int lastIndexOf(Object o)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public ListIterator<T> listIterator()
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public ListIterator<T> listIterator(int index)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public List<T> subList(int fromIndex, int toIndex)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public Object clone()
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public void forEach(Consumer<? super T> action)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public Spliterator<T> spliterator()
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean removeIf(Predicate<? super T> filter)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public void replaceAll(UnaryOperator<T> operator)
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public void sort(Comparator<? super T> c)
    {
        throw new UnsupportedOperationException();
    }
}
